<?php
/*if ($_GET['id_ad'] == "0")
{
    include('insert_advert.php');
}
if ($_GET['id_ad'] == "1")
{
    include('edit_advert.php');
}*/
include('admin_advert.php');
    /**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/12/2013
 * Time: 11:34 πμ
 */

if ($_GET['id_ad'] == "0")
{
if ($_POST['click'] == "1")
{
    $sql_q = 'SELECT * FROM category WHERE name="'.$_POST['name'].'"';
    $result = mysqli_query($con, $sql_q);
    $row = mysqli_fetch_array($result);
    $_name="";
    $__name="";
    //echo $sql_q.'<br>';
    if (!empty($row['name']))
    {
        $_name='Το name υπάρχει! Η εισαγωγή δεν έγινε.';
    }
    $sql_q = 'SELECT * FROM category WHERE divname="'.$_POST['divname'].'"';
    $result = mysqli_query($con, $sql_q);
    $row = mysqli_fetch_array($result);
    //echo $sql_q.'<br>';
    if (!empty($row['divname']))
    {
        $__name='Το divname υπάρχει! Η εισαγωγή δεν έγινε.';
    }
    $tmp = trim($_POST['name']);
    if (empty($tmp))
    {
        $_name = 'Δεν επιτρέπονται τα κενά name!';
    }
    $tmp = trim($_POST['divname']);
    if (empty($tmp))
    {
        $__name = 'Δεν επιτρέπονται τα κενά divname!';
    }
    if (empty($_name) and empty($__name))
    {
        $sql_q = 'INSERT INTO category (`id`,`name`,`divname`) VALUES (NULL,"'.$_POST['name'].'","'.$_POST['divname'].'")';
        $result = mysqli_query($con, $sql_q);
    }
    echo $_name.'<br>'.$__name;
}

$click = "0";
$_POST["click"] = "0";


$form = '<h4>Εισαγωγή νέας κατηγορίας</h4>';
$form = $form . '<form id="form_category_insert" method="post">';
$form = $form . '<label> Name</label>';
$form = $form . '<input name="name" type="text" value=""/>';
$form = $form . '<label> Divname</label>';
$form = $form . '<input name="divname" type="text" value=""/>';
//$form = $form . '<label> Parent_id</label>';
//$form = $form . '<select name="parent_id"/>';
//$form = $form . '<option value="0" selected="selected"</option>';
//while($row = mysqli_fetch_array($result))
//{
//    $form = $form . '<option value="'.$row['id'].'">'.$row['name'].'</option>';
//}
//$form = $form . '</select>';
//$form = $form . '<label> menu_id</label>';
//$form = $form . '<select name="menu_id"/>';
//$form = $form . '<option value="0" selected="selected"</option>';
//$result = mysqli_query($con, " SELECT * FROM menu group by name ");
//while($row = mysqli_fetch_array($result))
//{
//    $form = $form . '<option value="'.$row['id'].'">'.$row['name'].'</option>';
//}
//$form = $form . '<option value="-1">Όλα</option>';
//$form = $form . '</select>';
$form = $form . '<input name="click" type="hidden" value="1"/>';
$form = $form . '<input type="submit" value="Προσθήκη κατηγορίας"/>';
$form = $form . '</form>';
echo $form;
}
elseif ($_GET['id_ad'] == "1") {
$result = mysqli_query($con, " SELECT * FROM category group by name ");
echo '<table border="1">';
while($row = mysqli_fetch_array($result))
{
    echo '<tr><td>'.$row['id'].'</td><td>'.$row['name'].'</td><td>'.$row['divname'].'</td><td>[ <a href="?page=admin_advert/category_edit&id_ad=2&id='.$row['id'].'">Επεξεργασία</a> ]</td><td>[ <a href="?page=admin_advert/category_edit&id_ad=3&id='.$row['id'].'">Διαγραφή</a> ]</td></tr>';
}
echo '</table>';
}
else
{
    echo "<br><h2>PAGE NOT FOUND</h2>";
}
?>